package com.bflow.web.controller;

import com.bflow.api.IBflowRunTaskService;
import com.bflow.api.bo.BflowRunTaskAuditBo;
import com.bflow.api.bo.ext.BflowUserBo;
import com.bflow.api.constant.BflowConstant.ActAssignTypes;
import com.bflow.util.AssertUtil;
import com.bflow.web.util.TestLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Name 任务管理
 * @Author haowen
 * @Date 2019-01-08
 */
@Controller
@RequestMapping(value = "/bflow/task")
public class BflowTaskController extends BflowBaseController {

    @Autowired
    private IBflowRunTaskService bflowRunTaskService;

    /**
     * 分派给用户
     * @param taskId
     * @param userId
     * @return
     */
    @RequestMapping(value = "assignToUser")
    @ResponseBody
    public String assignToUser(String taskId, String userId, HttpServletRequest req, HttpServletResponse resp) {
        BflowUserBo user = TestLoginUtil.getUser(req);
        AssertUtil.isNotNull(user, "您还未登录, 请先登录");
        AssertUtil.isNotNull(taskId, "请求参数有误, 请联系管理员");
        AssertUtil.isNotNull(userId, "请求参数有误, 请联系管理员");
        BflowRunTaskAuditBo auditBo = new BflowRunTaskAuditBo(taskId, user);
        try {
            bflowRunTaskService.assign(auditBo, ActAssignTypes.ASSIGN_USER_ID, userId);
        } catch (Exception e) {
            return returnFail(e, "分派失败, 请联系管理员");
        }
        return returnOk("分派成功");
    }

    /**
     * 分派给 角色
     * @param taskId
     * @param roleKey
     * @return
     */
    @RequestMapping(value = "assignToRole")
    @ResponseBody
    public String assignToRole(String taskId, String roleKey, HttpServletRequest req, HttpServletResponse resp) {
        BflowUserBo user = TestLoginUtil.getUser(req);
        AssertUtil.isNotNull(user, "您还未登录, 请先登录");
        AssertUtil.isNotNull(taskId, "请求参数有误, 请联系管理员");
        AssertUtil.isNotNull(roleKey, "请求参数有误, 请联系管理员");
        BflowRunTaskAuditBo auditBo = new BflowRunTaskAuditBo(taskId, user);
        try {
            bflowRunTaskService.assign(auditBo, ActAssignTypes.ASSIGN_ROLE_KEY, roleKey);
        } catch (Exception e) {
            return returnFail(e, "分派失败, 请联系管理员");
        }
        return returnOk("分派成功");
    }

    /**
     * 转交给用户
     * @param taskId
     * @param userId
     * @return
     */
    @RequestMapping(value = "transferToUser")
    @ResponseBody
    public String transferToUser(String taskId, String userId, String remarks, HttpServletRequest req, HttpServletResponse resp) {
        BflowUserBo user = TestLoginUtil.getUser(req);
        AssertUtil.isNotNull(user, "您还未登录, 请先登录");
        AssertUtil.isNotNull(taskId, "请求参数有误, 请联系管理员");
        AssertUtil.isNotNull(userId, "请求参数有误, 请联系管理员");
        BflowRunTaskAuditBo auditBo = new BflowRunTaskAuditBo(taskId, user);
        auditBo.setRemarks(remarks);
        try {
            bflowRunTaskService.transfer(auditBo, ActAssignTypes.TRANSFER_USER_ID, userId);
        } catch (Exception e) {
            return returnFail(e, "转交失败, 请联系管理员");
        }
        return returnOk("转交成功");
    }
}
